﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SpringStudio.Common;
using SpringStudio.DAL;
using Sdepci.InfoCode.BLL.Authority;

namespace Sdepci.InfoCode.Web.Authority
{
    public partial class FunctionList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.ViewState["MyGridTemplate"] = this.MyGridTemplate.InnerHtml;
                this.BindData();
            }
        }

        /// <summary>
        /// 绑定数据列表
        /// </summary>
        private void BindData()
        {
            string parentFuncGuid = "0";
            if (this.Request.QueryString["ParentFuncGuid"] != null && !"".Equals(this.Request.QueryString["ParentFuncGuid"].Trim()))
            {
                parentFuncGuid = this.Request.QueryString["ParentFuncGuid"].Trim();
            }
            if("0".Equals(parentFuncGuid))
            {
                this.btnBack.Visible = false;
            }

            DataSet ds = new FunctionManager().GetFunctionDs(parentFuncGuid);

            this.MyGridTemplate.InnerHtml = MyGrid.DsToHtml(ds, this.ViewState["MyGridTemplate"].ToString());
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.Request.QueryString["ParentFuncGuid"] != null && !"".Equals(this.Request.QueryString["ParentFuncGuid"].Trim()))
            {
                this.Response.Redirect("FunctionEdit.aspx?ParentFuncGuid=" + this.Request.QueryString["ParentFuncGuid"].Trim());
            }
            else
            {
                this.Response.Redirect("FunctionEdit.aspx");
            }
        }

        protected void btnModify_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("FunctionEdit.aspx?FuncGuid=" + this.Request.Form["ItemId"].ToString().Split(',')[0]);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.Request.Form["ItemId"] != null)
            {
                FunctionManager functionManager = new FunctionManager();
                string[] funcGuids = this.Request.Form["ItemId"].ToString().Split(',');

                bool allDelete = true;
                for (int i = 0; i < funcGuids.Length; i++)
                {
                    if (functionManager.ExistChild(funcGuids[i]))
                    {
                        allDelete = false;
                    }
                    else
                    {
                        functionManager.Delete(funcGuids[i]);
                    }
                }

                string msg = string.Empty;
                if (allDelete)
                {
                    msg = "删除完成！";
                }
                else
                {
                    msg = "删除完成！但某些菜单下还存在子菜单，所以未被删除！";
                }

                if (this.Request.QueryString["ParentFuncGuid"] != null && !"".Equals(this.Request.QueryString["ParentFuncGuid"].Trim()))
                {
                    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script language=\"javascript\">alert(\"" + msg + "！\");document.location.href='FunctionList.aspx?ParentFuncGuid=" + this.Request.QueryString["ParentFuncGuid"].Trim() + "';</script>");
                }
                else
                {
                    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script language=\"javascript\">alert(\"" + msg + "！\");document.location.href='FunctionList.aspx';</script>");
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("FunctionList.aspx?ParentFuncGuid=" + new Function(this.Request.QueryString["ParentFuncGuid"].Trim()).ParentFuncGuid);
        }
    }
}
